const express = require("express");
const multer = require("multer");
const path = require('path');

const router = express.Router();

// 头像上传
const headphotoStorage = multer.diskStorage({
    // 设置上传文件存储位置
    destination: function (req, file, cb) {
        cb(null, path.join(__dirname, '../static/headphoto'))
    },
    // 最后存储时候的文件名
    filename: function (req, file, cb) {
        cb(null, `${Date.now()}-${file.originalname}`)
    },
})
const headphotoUpload = multer({ storage: headphotoStorage })

router.post('/headphoto', headphotoUpload.single('headphoto'), function (req, res) {
    res.send({
        code: 0,
        msg: 'ok',
        data: `/static/headphoto//${req.file.filename}` // 当前文件的地址
    })
})

// 封面上传
const coverStorage = multer.diskStorage({
    // 设置上传文件存储位置
    destination: function (req, file, cb) {
        cb(null, path.join(__dirname, '../static/cover'))
    },
    // 最后存储时候的文件名
    filename: function (req, file, cb) {
        cb(null, `${Date.now()}-${file.originalname}`)
    },
})
const coverUpload = multer({ storage: coverStorage })

router.post('/cover', coverUpload.single('cover'), function (req, res) {
    res.send({
        code: 0,
        msg: 'ok',
        data: `/static/cover//${req.file.filename}` // 当前文件的地址
    })
})

// 日记内图片上传
const photoStorage = multer.diskStorage({
    // 设置上传文件存储位置
    destination: function (req, file, cb) {
        cb(null, path.join(__dirname, '../static/photo'))
    },
    // 最后存储时候的文件名
    filename: function (req, file, cb) {
        cb(null, `${Date.now()}-${file.originalname}`)
    },
})
const photoUpload = multer({ storage: photoStorage })

router.post('/photo', photoUpload.single('photo'), function (req, res) {
    res.send({
        code: 0,
        msg: 'ok',
        data: `/static/photo//${req.file.filename}` // 当前文件的地址
    })
})


module.exports = router;